﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.IO;
using Newtonsoft.Json;
using System.Threading;

namespace DSEngineAnimationMarker
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        AnimationEntity animation;
        int texturewidth;
        int textureheight;
        Image fromImage;
        private void btnStart_Click(object sender, EventArgs e)
        {
            StreamReader sr=new StreamReader("jumping.animation");
            string animationstr = sr.ReadToEnd();

            animation = JsonConvert.DeserializeObject<AnimationEntity>(animationstr);
            fromImage = Image.FromFile("jumping_texture.png");
            texturewidth = fromImage.Width;
            textureheight= fromImage.Height;


            Thread thrad = new Thread(new ThreadStart(() => {
                try
                {
                    int i = 0;
                    un = delegate
                    {
                        lblnowframeindex.Text = i + "";
                        for (int j = 0; j < animation.frames.Count; j++)
                        {
                            if (animation.frames[j].name == animation.animations[0].key_frames[i].frame)
                            {
                                pictureBox1.Image = CaptureImage(fromImage, (int)(animation.frames[j].size.x * texturewidth), (int)(animation.frames[j].size.y * textureheight), (int)(animation.frames[j].offset.x * texturewidth), (int)(animation.frames[j].offset.y * textureheight));
                                break;
                            }
                        }

                    };
                    for (; i < animation.animations[0].key_frames.Count;i++ )
                    {
                        this.Invoke(un);
                        Thread.Sleep((int)((animation.animations[0].key_frames[i].duration) * 1000));
                    }
                }
                catch { }
            }));
            thrad.Start();
            
        }

        delegate void UnName();
        UnName un;

        ///   <summary> 
        ///   从图片中截取部分生成新图 
        ///   </summary> 
        ///   <param   name= "sFromFilePath "> 原始图片 </param> 
        ///   <param   name= "saveFilePath "> 生成新图 </param> 
        ///   <param   name= "width "> 截取图片宽度 </param> 
        ///   <param   name= "height "> 截取图片高度 </param> 
        ///   <param   name= "spaceX "> 截图图片X坐标 </param> 
        ///   <param   name= "spaceY "> 截取图片Y坐标 </param> 
        public Image CaptureImage(Image fromImage, int width, int height, int spaceX, int spaceY)
        {
            //载入底图 
            int x = 0;   //截取X坐标 
            int y = 0;   //截取Y坐标 
            //原图宽与生成图片宽   之差     
            //当小于0(即原图宽小于要生成的图)时，新图宽度为较小者   即原图宽度   X坐标则为0   
            //当大于0(即原图宽大于要生成的图)时，新图宽度为设置值   即width         X坐标则为   sX与spaceX之间较小者 
            //Y方向同理 
            int sX = fromImage.Width - width;
            int sY = fromImage.Height - height;
            if (sX > 0)
            {
                x = sX > spaceX ? spaceX : sX;
            }
            else
            {
                width = fromImage.Width;
            }
            if (sY > 0)
            {
                y = sY > spaceY ? spaceY : sY;
            }
            else
            {
                height = fromImage.Height;
            }

            //创建新图位图 
            Bitmap bitmap = new Bitmap(width, height);
            //创建作图区域 
            Graphics graphic = Graphics.FromImage(bitmap);
            //截取原图相应区域写入作图区 
            graphic.DrawImage(fromImage, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
            //从作图区生成新图 
            Image saveImage = Image.FromHbitmap(bitmap.GetHbitmap());
            return saveImage;
            //保存图象 
            //saveImage.Save(saveFilePath, ImageFormat.Jpeg);
            //释放资源 
            saveImage.Dispose();
            bitmap.Dispose();
            graphic.Dispose();
        }

        
    }
}
